package com.bangdream.michelia.view.activity.exam;

import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.andview.refreshview.XRefreshView;
import com.bangdream.michelia.R;
import com.bangdream.michelia.application.AppCurrentUser;
import com.bangdream.michelia.entity.BaseEntity;
import com.bangdream.michelia.entity.exam.ExamBean;
import com.bangdream.michelia.presenter.base.BasePresenter;
import com.bangdream.michelia.tool.retrofithttp.RetroFactory;
import com.bangdream.michelia.tool.retrofithttp.TokenManagerDef;
import com.bangdream.michelia.utils.L;
import com.bangdream.michelia.utils.ToastUtil;
import com.bangdream.michelia.view.activity.base.SwipeBackActivity;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * @date on 2018/8/17 19:05
 * @email 909732184@qq.com
 * 考试说明
 */
public class ExamExplainActivity extends SwipeBackActivity {
    private XRefreshView refreshView;
    private TextView tvTitle,tvDate,tvTime,tvTotalScore,tvPassScore,tvCredit;
    private ExamBean bean;
    private String paperId;
    private final int ExamCode = 0x145;
    String planUserItemId="";
    private boolean isPlan=false;
    String examId="";
    @Override
    public int getLayout() {
        return R.layout.activity_exam_explan;
    }

    @Override
    public void findId() {
        refreshView = findViewById(R.id.refreshView);
        tvTitle = findViewById(R.id.tvTitle);
        tvDate = findViewById(R.id.tvDate);
        tvTime = findViewById(R.id.tvTime);
        tvTotalScore = findViewById(R.id.tvTotalScore);
        tvPassScore = findViewById(R.id.tvPassScore);
        tvCredit = findViewById(R.id.tvCredit);
        refreshView.setPullLoadEnable(false);
        refreshView.setPullRefreshEnable(false);
    }

    @Override
    public void setListener() {

    }

    @Override
    public void logic() {
        initTitleBar(" ","考前说明","",this);
//        if(getIntent().getExtras()!=null){
//            bean = getIntent().getExtras().getParcelable("data");
//            paperId = bean.getId();
//        }
//        setViewData();

        if(getIntent().getExtras()!=null){
            examId = getIntent().getStringExtra("examId");
            Log.e("examId","++++++++++"+examId);
            bean = getIntent().getExtras().getParcelable("data");
            if(bean!=null) {
                paperId = bean.getId();
                setViewData();
            }
        }
        if(getIntent().getStringExtra("itemId")!=null){
            planUserItemId = getIntent().getStringExtra("planUserItemId");
            isPlan=true;
            getData(getIntent().getStringExtra("itemId"));
        }

    }

    private void setViewData() {
        if(bean==null)
            return;
        tvTitle.setText(bean.getTitle()+"");
        tvTime.setText(bean.getTime()+"分钟");
        tvPassScore.setText(bean.getPassline()+"分");
        tvTotalScore.setText(bean.getScore()+"分");

//        tvCredit.setText(bean.getUserCntTest()+"分");
        tvCredit.setText(bean.getCntQuestion()+"分");
        tvDate.setText(bean.getStartDate()+"至 "+bean.getEndDate()+"");
    }

    @Override
    public BasePresenter createPresenter() {
        return null;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btnGo:
                Intent intent = new Intent(this,ExamActivity.class);
                intent.putExtra("paperId",paperId);
                intent.putExtra("isPlan",isPlan);
                intent.putExtra("data",bean);
                intent.putExtra("examId",examId);
                Log.e("examId","++++++++++"+examId);
                intent.putExtra("isContinue",getIntent().getBooleanExtra("isContinue",false));
                intent.putExtra("planUserItemId",planUserItemId);
                startActivityForResult(intent,ExamCode);
                break;
                default:break;
        }
        super.onClick(view);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode!= Activity.RESULT_OK)
            return;
        switch (requestCode){
            case ExamCode:
                isRefresh = true;
                finish();
                break;
        }
    }

    private void getData(String id){
        Map map=new HashMap();
        map.put("type", "1");
        map.put("id", id);

        String jsonString = JSON.toJSONString(map);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"),jsonString);

        TokenManagerDef.work(() ->
                RetroFactory.getInstance().getExamList(AppCurrentUser.getInstance().getToken(),requestBody))
                .compose(TokenManagerDef.newInstance().getTransformer())
                .compose(composeFunction)
                .subscribe(new Observer<BaseEntity<List<ExamBean>>>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(BaseEntity<List<ExamBean>> o) {
                        pd.cancel();
                        if(o.getData()!=null && !o.getData().isEmpty()){
                            bean=o.getData().get(0);
                            paperId = bean.getId();
                            setViewData();
                        }else{
                            ToastUtil.showShortToast(ExamExplainActivity.this,"暂无数据");
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        pd.cancel();
                        ToastUtil.showShortToast(ExamExplainActivity.this,"获取数据失败");
                    }

                    @Override
                    public void onComplete() { }
                });
    }
}
